package com.yipao.admin.web.dev;

import com.google.zxing.WriterException;
import com.yipao.admin.vo.dev.QrCodeVO;
import com.yipao.core.qcode.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 二维码
 *
 * @author tianhao
 * @date 2020/5/23 16:07
 **/
@Slf4j
@RestController
@RequestMapping("/admin/devInfo")
public class QrCodeController {
    @Value("${file.qRCode.scheme}")
    private String qRCodeScheme;

    /**
     * 查看智能柜二维码
     * 前端传deviceNo 设备编号
     *
     * @param vo
     * @param response
     */
    @RequestMapping("/QRCode")
    public void qRCode(QrCodeVO vo, HttpServletResponse response) {
        try {
            if (StringUtils.isNotBlank(vo.getFileName())) {
                response.setHeader("Content-Disposition", "attachment; filename=" + vo.getFileName() + "." + vo.getImgFormat());
            }
            QRCodeUtil.createQrCode(response.getOutputStream(), qRCodeScheme + vo.getImei(), vo.getSize(), vo.getImgFormat());
        } catch (WriterException e) {
            log.error("生成设备二维码异常", e);
        } catch (IOException e) {
            log.error("生成设备二维码异常", e);
        }
    }
}
